package com.sansi.pencilbook.mapper;

import com.sansi.pencilbook.entity.Book;
import com.sansi.pencilbook.entity.Category;
import com.sansi.pencilbook.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明: 用户的增删改查
 *
 * @author lsy13
 */
@Mapper
public interface UserMapper {

    @Select("select user_id, user_account, user_password, user_name, user_sex, user_age from user")
    public List<User> findAll() throws SQLException;

    @Select("select user_id, user_account, user_password, user_name, user_sex, user_age from user " +
            "where user_account=#{account}")
    public User findUserByAccount(String account) throws SQLException;

    @Insert("insert into user (user_account,user_password,user_name,user_sex,user_age) " +
            "values(#{userAccount}, #{userPassword}, #{userName}, #{userSex}, #{userAge})")
    public int saveUser(User user) throws SQLException;

    @Update("update user set user_password=#{userPassword}," +
            "user_name=#{userName}, user_sex=#{userSex}, user_age=#{userAge} " +
            "where user_account=#{userAccount}")
    public int updateUser(User user) throws SQLException;

    @Delete("delete from user where user_account=#{userAccount}")
    public int deleteUser(User user) throws SQLException;

    @Delete("<script>" +
            "delete from user where user_account in " +
            "<foreach collection='users' item='user' index='index' open='(' separator=',' close=')'>" +
            "#{user.userAccount}" +
            "</foreach>" +
            "</script>")
    public int deleteUsers(List<User> users) throws SQLException;
}
